<?php
/* OpenBIBLIO -- A library administration web interface
 * Copyright (C) 2002-2005 Fr&eacute;d&eacute;ric Descamps <lefred@tiscali.be>
 * Copyright (C) 2011-     Laurent Luyssen   <big.lol13@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

include ("jpgraph/jpgraph.php");
include ("jpgraph/jpgraph_pie.php");
include ("jpgraph/jpgraph_pie3d.php");

include_once("../includes/db.inc");
include_once("../includes/functions.inc");
include_once("../includes/locale.inc");

if(!isset($_GET['type'])) 
	$type=1;
else 
	$type=($_GET['type']);

switch ($type) {
 case 1:
     $requete = "select count(*) tot, genres.genre from livres inner join genres on genres.id = livres.genre_ID where del=0 group by genres.id order by tot desc;";
     $nom="genre";
     break;
 case 2:
     $requete = "select count(*) tot, etat from livres inner join etats on etats.id = livres.etat_ID where del=0 group by etats.id order by tot desc;";
     $nom="etat";
     break;
 case 3:
     $requete = "SELECT count(*) as tot, loc.local as local FROM locations loc, livres liv WHERE liv.del = 0 AND liv.location_Id = loc.id  GROUP BY loc.local ORDER BY tot DESC";
     break;
 case 4:
     $requete = "SELECT count(*) as tot, loc.local as local, loc.biblio as biblio FROM locations loc, livres liv WHERE liv.del = 0 AND liv.location_Id = loc.id  GROUP BY loc.local, loc.biblio ORDER BY tot DESC";
     break;
 case 5:
     $requete = "SELECT count(*) as tot, loc.local as local, loc.biblio as biblio, loc.rangee as rangee FROM locations loc, livres liv WHERE liv.del = 0 AND liv.location_Id = loc.id  GROUP BY loc.local, loc.biblio, loc.rangee ORDER BY tot DESC";
     break;
}

$i = 0;
$tot = 0;
$resultat = biblio_db_query($requete);
if($resultat) {
	while ($row = biblio_db_fetch_array($resultat)) {
		$i++;
		if ($i < 15) {
			$valeurs[] = $row['tot'];
			switch ($type) {
				case 3:
					$champs[] = get_location_string($row['local'])." (".$row['tot'].")";
					break;
				case 4:
					$champs[] =get_location_string($row['local'], $row['biblio'])." (".$row['tot'].")";
					break;
				case 5:
					$champs[] = get_location_string($row['local'], $row['biblio'], $row['rangee'])." (" . $row['tot'] .")";
					break;
				default:
					$champs[] = $row[$nom]." (".$row['tot'].")"; 
			}
		}
		else 
			$tot += $row['tot'];
	}
	if ($tot > 0) {
		$champs[] = _("Autres")." ($tot)";
		$valeurs[] = $tot;
	}
	// Cr�ation d'un nouveau camembert de 500px sur 200px
	$graph = new PieGraph(800,400,"auto");
	// On sp�cifie que l'on veut une ombre
	$graph->SetShadow();
	// Cr�ation du camembert 3D en lui passant les valeurs
	$p = new PiePlot3d($valeurs);
	// D�finition du th�me correspondant � une s�rie de couleurs qui seront utilis�es
	// Trois th�mes existent : earth, pastel, sand et water
	$p->SetTheme("sand");
	// D�finition de la position du centre du camembert
	$p->SetCenter(0.35);
	// D�finition de la taille du camembert
	$p->SetSize(250);
	// Affichage des valeurs absolues. Par d�faut, les valeurs sont recalcul�es en
	// pourcentage pour obtenir un total de 100
	$p->SetLabelType("PIE_VALUE_ABS");
	// Ajustement de l'angle de projection de 55�
	$p->SetAngle(55);
	// Ajustement de l'angle de d�part � 45�
	$p->SetStartAngle(45);
	// D�finition de la couleur des valeurs affich�es
	$p->value->SetColor("navy");
	// D�finition de la couleur des bordures du camembert
	$p->SetEdge("navy");
	// Initialisation de la l�gende
	$p->SetLegends($champs);
	$graph->legend->Pos(0.015,0.015); 
	// Ajout du camembert 3D au graph
	$graph->Add($p);
	// G�n�ration de l'image en streaming
	$graph->Stroke();
}

?>
